<?php
 
function getBeginingDate($currentDate) {	
	$currentDateDivision = explode ("-", $currentDate);
	$currentDay = $currentDateDivision[0];
	$currentMonth = $currentDateDivision[1];
	$currentYear = $currentDateDivision[2];
	$nextClosure = '01-' . $currentMonth . "-" . $currentYear;		
	return date("d-m-Y", strtotime($nextClosure));		
}
function getEndingDate($currentDate) {	
	$endingDay[1] = 31;
	$endingDay[2] = 28;
	$endingDay[3] = 31;
	$endingDay[4] = 30;
	$endingDay[5] = 31;
	$endingDay[6] = 30;
	$endingDay[7] = 31;
	$endingDay[8] = 31;
	$endingDay[9] = 30;
	$endingDay[10] = 31;
	$endingDay[11] = 30;
	$endingDay[12] = 31;
	$currentDateDivision = explode ("-", $currentDate);
	$currentDay = $currentDateDivision[0];
	$currentMonth = $currentDateDivision[1];
	$currentYear = $currentDateDivision[2];
	$nextClosure = $endingDay[(int)$currentMonth] . '-' . $currentMonth . "-" . $currentYear;		
	return date("d-m-Y", strtotime($nextClosure));		
}

function getLastMonthBeginingDate($currentDate) {	
	$currentDateDivision = explode ("-", $currentDate);
	$currentDay = $currentDateDivision[0];
	$currentMonth = $currentDateDivision[1];
	$currentYear = $currentDateDivision[2];
	if ($currentMonth > 1) {
		$lastMonth = $currentMonth - 1;
	}else{
		$lastMonth = 12;
	}
	if ($lastMonth < 10) {
		$nextClosure = '01-0' . $lastMonth . "-" . $currentYear;
	}else{
		$nextClosure = '01-' . $lastMonth . "-" . $currentYear;	
	}
	return date("d-m-Y", strtotime($nextClosure));		
}

function getLastMonthEndingDay($currentDate) {
	$endingDay[1] = 31;
	$endingDay[2] = 28;
	$endingDay[3] = 31;
	$endingDay[4] = 30;
	$endingDay[5] = 31;
	$endingDay[6] = 30;
	$endingDay[7] = 31;
	$endingDay[8] = 31;
	$endingDay[9] = 30;
	$endingDay[10] = 31;
	$endingDay[11] = 30;
	$endingDay[12] = 31;
	
	$currentDateDivision = explode ("-", $currentDate);
	$currentDay = $currentDateDivision[0];
	$currentMonth = $currentDateDivision[1];
	$currentYear = $currentDateDivision[2];
	
	if ($currentMonth > 1) {
		$lastMonth = $currentMonth - 1;
	}else{
		$lastMonth = 12;
	}
	if ($lastMonth < 10) {
		$nextClosure = $endingDay[(int)$lastMonth] . '-0' . $lastMonth . "-" . $currentYear;
	}else{
		$nextClosure = $endingDay[(int)$lastMonth] . '-' . $lastMonth . "-" . $currentYear;	
	}
	return date("d-m-Y", strtotime($nextClosure));		
}

function getLastChargeDate($user) {
	include('dataBaseConnection.php');
	 $result = mysqli_query($connection, "SELECT date FROM usersworkedtime WHERE user = '$user' order by date DESC"); 
			$row = mysqli_fetch_array($result); 
			if ($row != NULL) {
				foreach ($row as $day){
					$lastCharge = date("d-m-Y", strtotime($day));
					break;
				}	
			}else{
				$lastCharge = "no hay cargas registradas";
			}
	return $lastCharge;
}

function getLastMonthLastChargeDay($user, $date) {
	include('dataBaseConnection.php');
	$lastMonthBeginingDay = date("Y-m-d", strtotime(getLastMonthBeginingDate($date)));
	$lastMonthEndingDay = date("Y-m-d", strtotime(getLastMonthEndingDay($date)));
	$query = "SELECT date FROM usersworkedtime WHERE user = '$user' AND date BETWEEN '$lastMonthBeginingDay' AND '$lastMonthEndingDay' order by date DESC";
	$result = mysqli_query($connection, $query); 
	$row = mysqli_fetch_array($result); 
	if ($row != NULL) {
		foreach ($row as $day){
		$lastMonthLastChargeDay = date("d-m-Y", strtotime($day));
			break;
		}	
	}else{
		$lastMonthLastChargeDay = date("d-m-Y", strtotime($lastMonthBeginingDay));
			}
	return $lastMonthLastChargeDay;
}

function getMonth($date) {	
	$currentDateDivision = explode ("-", $date);
	$currentDay = $currentDateDivision[0];
	$currentMonth = $currentDateDivision[1];
	$currentYear = $currentDateDivision[2];
	return $currentMonth;
}

function getPrevMonth($date) {
	$currentDateDivision = explode ("-", $date);
	$currentDay = $currentDateDivision[0];
	$currentMonth = $currentDateDivision[1];
	$currentYear = $currentDateDivision[2];
	
	if ($currentMonth > 1) {
		$lastMonth = $currentMonth - 1;
	}else{
		$lastMonth = 12;
	}
	
	return $lastMonth;
}

	function getMonthName($month) {
		$months[1] = "Enero";
		$months[2] = "Febrero";
		$months[3] = "Marzo";
		$months[4] = "Abril";
		$months[5] = "Mayo";
		$months[6] = "Junio";
		$months[7] = "Julio";
		$months[8] = "Agosto";
		$months[9] = "Septiembre";
		$months[10] = "Octubre";
		$months[11] = "Noviembre";
		$months[12] = "Diciembre";
		return $months[(int)$month];
	}

/**
 * Metodo getDiasHabiles
 * 
 * Permite devolver un arreglo con los dias habiles
 * entre el rango de fechas dado excluyendo los
 * dias feriados dados (Si existen)
 * 
 * @param string $fechainicio Fecha de inicio en formato Y-m-d
 * @param string $fechafin Fecha de fin en formato Y-m-d
 * @param array $diasferiados Arreglo de dias feriados en formato Y-m-d
 * @return array $diashabiles Arreglo definitivo de dias habiles
 */
function getDiasHabiles($fechainicio, $fechafin, $diasferiados = array()) {
	// Convirtiendo en timestamp las fechas
	$fechainicio = strtotime($fechainicio);
	$fechafin = strtotime($fechafin);
	// Incremento en 1 dia
	$diainc = 24*60*60;
	
	// Arreglo de dias habiles, inicianlizacion
	$diashabiles = array();
	
	// Se recorre desde la fecha de inicio a la fecha fin, incrementando en 1 dia
	for ($midia = $fechainicio; $midia <= $fechafin; $midia += $diainc) {
	
	
		// Si el dia indicado, no es sabado o domingo es habil
		if (!in_array(date('N', $midia), array(6,7))) { // DOC: http://www.php.net/manual/es/function.date.php
			// Si no es un dia feriado entonces es habil
			if (!in_array(date('Y-m-d', $midia), $diasferiados)) {
				array_push($diashabiles, date('Y-m-d', $midia));
			}
		}
	}
	
	return $diashabiles;
}

?>

